﻿  using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Data;
using System.Data.SQLite;
using System.Threading;
using System.Diagnostics;
using System.Windows.Forms;
using WattsUpComponent;
using MainDisplay;
using System.IO;


namespace MosaicPowerMonitor2
{
    class Controller
    {
        public static SQLiteConnection coeff;
        public static PerformanceCounter cpuCounter;

        public static void SetConnection()
        {
            String connectionString = "data source=" + "C:\\Mosaic\\Coefficients" + ";Version=3;";
            coeff = new SQLiteConnection(connectionString);
        }

        [STAThread]
        static void Main()
        {

         
            SetConnection();
            try
            {
                coeff.Open();
            }
            catch (Exception)
            {
                Console.WriteLine("not open");
            }
            SQLiteCommand create1 = new SQLiteCommand("CREATE TABLE IF NOT EXISTS Main(Model VARCHAR(50), Alpha decimal , Baseline decimal)",coeff);
            create1.ExecuteNonQuery();
            SQLiteCommand readalpha = new SQLiteCommand("SELECT Alpha FROM Main WHERE (Model = 'w4')", coeff);
            object value = readalpha.ExecuteScalar();
            SQLiteCommand readbaseline = new SQLiteCommand("SELECT Baseline FROM Main WHERE (Model = 'w4')", coeff);
            object value2 = readbaseline.ExecuteScalar();

           // double alpha = System.Convert.ToDouble(value.ToString());
           // double baseline = System.Convert.ToDouble(value2.ToString());
            double alpha = 0.196;
            double baseline = 14.841;
            SQLiteCommand create = new SQLiteCommand("CREATE TABLE IF NOT EXISTS Power(Time DATETIME DEFAULT Current_Time, Power FLOAT)", coeff);
            create.ExecuteNonQuery();
           // create = new SQLiteCommand("CREATE TABLE IF NOT EXISTS month(Time DATETIME DEFAULT Current_Time, Power FLOAT)", coeff);
          //  create.ExecuteNonQuery();
            // create = new SQLiteCommand("CREATE TABLE IF NOT EXISTS two_weeks(Time DATETIME DEFAULT Current_Time, Power FLOAT)", coeff);
           // create.ExecuteNonQuery();


            Controller wind = new Controller();
            wind.newthread();
            int count = 0;

            while (true)
            {
                cpuCounter = new PerformanceCounter();
                cpuCounter.CategoryName = "Processor";
                cpuCounter.CounterName = "% Processor Time";
                cpuCounter.InstanceName = "_Total";
                double cpu = System.Convert.ToDouble(getCurrentCpuUsage());

                double power = alpha * cpu + baseline;
               // double actualP = Program.mainFunction(count); 
                Console.WriteLine("Alpha = " + alpha + " CPU Usage = " + cpu);
                SQLiteCommand storepower = new SQLiteCommand("INSERT INTO Power(Power) values ('" + power + "' )", coeff);
                storepower.ExecuteNonQuery();
                storepower = new SQLiteCommand("INSERT INTO two_weeks(Power) values ('" + power + "' )", coeff);
                storepower.ExecuteNonQuery();
                storepower = new SQLiteCommand("INSERT INTO month(Power) values ('" + power + "' )", coeff);
                storepower.ExecuteNonQuery();
                count++;
             
                
                Thread.Sleep(3000);
            }

        }

        public static float getCurrentCpuUsage()
        {
            cpuCounter.NextValue();
            Thread.Sleep(1000);
            return cpuCounter.NextValue();
        }

        public void newthread()
        {
            Thread thr = new Thread(new ThreadStart(StartNewStaThread));
            thr.Start();
        }

        private void StartNewStaThread()
        {
            Application.Run(new Mosaic());
        }



    }
}
